package cn.tedu.online.day03;

import java.util.*;

public class CharCountDescDemo {
    public static void main(String[] args) {
        System.out.println("请输入字符串:");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        //将str-->字符数组
        char[] ary = str.toCharArray();
        //创建HashMap集合,遍历字符数组,统计每个字符的次数
        Map<Character,Integer> map = new HashMap<>();
        for (char c:ary) {
            //判断c是否已经存在于map中
            if (map.containsKey(c)){  //已经存在
                map.put(c,map.get(c)+1);
            }else {  //第一次出现
                map.put(c,1);
            }
        }
//        map.forEach((k,v)-> System.out.println(k+":"+v));
        //对map根据value进行降序排列 -- 将map转换为list,对list进行自定义排序
        List<Map.Entry<Character,Integer>> list =
                new ArrayList<>(map.entrySet());
        list.sort(new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        list.forEach(e-> System.out.println(e));
    }
}
